package
{
	import flash.utils.ByteArray;
	
	import utils.NumberTool;
	public class ModBusCommand_YOUREN
	{
		private static const  end:String="ACAC";
		public static const  ask:String="01";
		private static const  control:String="05";
		public static const  open:String="FF";
		public static const  close:String="00";
		private static const  mutileControl:String="0F";
		public function ModBusCommand_YOUREN()
		{
		}
		private static function makeDeviceID(id:uint):String
		{
			//输出设备ID
			var str:String=id.toString(16);
			str=NumberTool.fullZeroWidthPosition(str,2,"l");
			return str;
		}
		
		private static function makeAddress(id:uint):String
		{
			//输出继电器地址
			var str:String=id.toString(16);
			str=NumberTool.fullZeroWidthPosition(str,4,"l");
			return str;
		}
		
		public static function openOneToString(deviceId:uint,id:uint):String
		{
			//打开一个继电器
			var str:String=makeDeviceID(deviceId)+control+makeAddress(id)+open+"00"+end;
			return str;
		}
		
		public static function closeOneToString(deviceId:uint,id:uint):String
		{
			//打开一个继电器
			var str:String=makeDeviceID(deviceId)+control+makeAddress(id)+close+"00"+end;
			return str;
		}
		
		public static function openOne(deviceId:uint,id:uint):ByteArray
		{
			//打开一个继电器
			var str:String=makeDeviceID(deviceId)+control+makeAddress(id)+open+"00"+end;
			trace("对设备"+deviceId+"的第"+(id+1)+"个继电器执行打开指令");
			return NumberTool.hexToByteArray(str);
		}
		
		public static function closeOne(deviceId:uint,id:uint):ByteArray
		{
			//关闭一个继电器
			var str:String=makeDeviceID(deviceId)+control+makeAddress(id)+close+"00"+end;
			trace("对设备"+deviceId+"的第"+(id+1)+"个继电器执行关闭指令");
			return NumberTool.hexToByteArray(str);
		}
		
		public static function controlMutileToString(diviceId:uint,startId:uint,length:uint,operate:String):String
		{
			var str:String=makeDeviceID(diviceId)+mutileControl+makeAddress(startId)+NumberTool.fullZeroWidthPosition(length.toString(16),4,"l")+"01"+NumberTool.fullZeroWidthPosition(NumberTool.twoTo16(operate),2,"l")+end;
			trace("对设备"+diviceId+" 从设备ID为"+(startId+1)+"开始,连续"+length+"个继电器执行"+operate+"指令");
			trace("指令转换为"+NumberTool.fullZeroWidthPosition(NumberTool.twoTo16(operate),2,"l"));
			return str;
		}
		
		public static function controlMutile(diviceId:uint,startId:uint,length:uint,operate:String):ByteArray
		{
			//操作多个连续的继电器
			
			var str:String=makeDeviceID(diviceId)+mutileControl+makeAddress(startId)+NumberTool.fullZeroWidthPosition(length.toString(16),4,"l")+"01"+NumberTool.fullZeroWidthPosition(NumberTool.twoTo16(operate),2,"l")+end;
			trace("对设备"+diviceId+" 从设备ID为"+(startId+1)+"开始,连续"+length+"个继电器执行"+operate+"指令");
			trace("指令转换为"+NumberTool.fullZeroWidthPosition(NumberTool.twoTo16(operate),2,"l"));
			return NumberTool.hexToByteArray(str);
		}
		
		public static function openAllStr(devideId:uint):String
		{
			//打开所有的继电器
			trace("对设备"+devideId+"的所有继电器执行打开指令");
			var str:String=makeDeviceID(devideId)+mutileControl+makeAddress(0)+"000801FF"+end;
			return str;
		}
		
		public static function openAll(diviceId:uint):ByteArray
		{
			//打开所有的继电器
			trace("对设备"+diviceId+"的所有继电器执行打开指令");
			var str:String=makeDeviceID(diviceId)+mutileControl+makeAddress(0)+"000801FF"+end;
			return NumberTool.hexToByteArray(str);
		}
		
		public static function closeAllStr(devideId:uint):String
		{
			//关闭所有的继电器
			trace("对设备"+devideId+"的所有继电器执行打开指令");
			var str:String=makeDeviceID(devideId)+mutileControl+makeAddress(0)+"00080100"+end;
			return str;
		}
		
		public static function closeAll(diviceId:uint):ByteArray
		{
			//关闭所有的继电器
			trace("对设备"+diviceId+"的所有继电器执行关闭指令");
			var str:String=makeDeviceID(diviceId)+mutileControl+makeAddress(0)+"00080100"+end;
			return NumberTool.hexToByteArray(str);
		}
	}
}